package com.cnspt.controllers;

import com.cnspt.models.Blog;
import com.cnspt.repository.BlogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogRepository blogRepository;

    @RequestMapping("/addBlog")
    public int addBlog() {
//        blogRepository.deleteAll();
        Blog blog = new Blog();
        blog.setId(1);
        blog.setTitle("睡晚了");
        blogRepository.save(blog);
        List<Blog> lists = blogRepository.findAll();
        System.out.println(lists.size());
        return lists.size();
    }

    @RequestMapping("/getBlogs")
    public List<Blog> getBlogs() {
        List<Blog> listsBlog = blogRepository.findAll();
        System.out.println(listsBlog.size());
        return listsBlog;
    }

    @RequestMapping("/delBlog")
    public List<Blog> delBlog() {
        blogRepository.deleteById(1);
        List<Blog> listsBlog = blogRepository.findAll();
        System.out.println(listsBlog.size());
        return listsBlog;
    }

    @RequestMapping("/findBlog")
    public Blog findBlog() {
        Blog blog = blogRepository.getBlogById(1);
        return blog;
    }

    @RequestMapping("/updateBlog")
    public Blog updateBlog() {
        Blog blog = new Blog();
        blog.setId(2);
        blog.setTitle("睡晚了132");
        blogRepository.save(blog);
        Blog blog1 = blogRepository.findAll().get(1);
        return blog1;
    }
}

